import os
import sys

# PATH vars

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = os.path.dirname(BASE_DIR)
VUE_APPFRONT = os.path.join(PROJECT_DIR, 'appfront', 'dist')

VERSION = 1.03  # forbidden cache
root = lambda *x: os.path.join(BASE_DIR, *x)
approot = lambda app, x: os.path.join(PROJECT_DIR, app, x)
appfront_root = lambda x: approot(VUE_APPFRONT, x)

sys.path.insert(0, root('apps'))

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'CHANGE THIS!!!'

# SECURITY WARNING: don't run with debug turned on in production!
IYINYUE_LOCAL = os.environ.get('IYINYUE', False) == 'local'
IYINYUE_TEST = os.environ.get('IYINYUE', False) == 'test'
DEBUG = True

ALLOWED_HOSTS = []

AUTH_PROFILE_MODULE = 'accounts.UserProfile'

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'rest_framework',
]

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'tools.err.roing_exception_handler'
}

PROJECT_APPS = ['school', 'teacher', 'student', 'accounts', 'common', 'wx', 'news', 'starday']

INSTALLED_APPS += PROJECT_APPS

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    "whitenoise.middleware.WhiteNoiseMiddleware",
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = (
    'http://127.0.0.1:8000',
)

ROOT_URLCONF = 'iyinyue.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'iyinyue.wsgi.application'

# Database

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'iyinyue',
        'USER': 'postgres',
        'PASSWORD': '123456',
        'HOST': '',  # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',  # Set to empty string for default.
    }
}

# Internationalization

LANGUAGE_CODE = 'en-gb'

TIME_ZONE = 'UTC'

USE_I18N = False

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)

STATIC_URL = '/static/'
STATIC_ROOT = root('static')
MEDIA_URL = '/media/'
MEDIA_ROOT = '/var/www/iyinyue/media/'

# Additional locations of static files

STATICFILES_DIRS = [
    root('assets'),
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'APP_DIRS': True,
        'DIRS': [
            root('templates'),
        ],
        'OPTIONS': {
            'debug': DEBUG,
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    }
]

STATICFILES_DIRS += [approot(app, 'assets') for app in PROJECT_APPS]
TEMPLATES[0]['DIRS'] += [approot(app, 'templates') for app in PROJECT_APPS]

# Password validation

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# .local.py overrides all the common settings.
try:
    if IYINYUE_LOCAL:
        from .local import *  # noqa
    else:
        from .production import *
except ImportError:
    pass


# importing test settings file if necessary
if IYINYUE_TEST:
    from .testing import *  # noqa
